home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug103 / roff2.c < prev    next >
Text File  |  1985-03-10  |  9KB  |  429 lines

  1. /* 7 MAY 81 */
  2.  
  3. #include "roffglob"
  4.  
  5. /****************************************************************
  6. handles case of leading blanks or tabs; empty lines 
  7. ******************************************************************/
  8.  
  9. leadbl (line)
  10. char *line;
  11. {
  12. int i, j;
  13.  
  14.  
  15. brk();
  16.  
  17. /* find first non-blank */
  18. for ( i=0; line[i] == BLANK; i++ )    ;
  19.  
  20. if ( line[i] != NEWLINE ) 
  21.     TIVAL = i;
  22. while (line[i] == TAB)
  23.       { TIVAL += 8;
  24.     i++;
  25.       }
  26.  
  27. /* move line to left */
  28. for (j=0; (line[j] = line[i]) != '\0'; j++, i++ );
  29.  
  30.  
  31. return;
  32. }
  33.  
  34.  
  35.  
  36. /*******************************************************************
  37. get indices of first and last occurrences of c in string
  38. ********************************************************************/
  39. int find_char (string, c, first, last)
  40. char *string, c;
  41. int *first, *last;
  42. {
  43. int i, j, k;
  44.  
  45. *first = -1;
  46. *last = -1;
  47. for (i=0; string[i] != '\0'; i++)
  48.       {
  49.     if (string[i] == c)
  50.           { *last = i;
  51.         if (*first == -1)    *first = i;
  52.           }
  53.       }
  54. return;
  55. }
  56.  
  57. /***************************************************************
  58.     replace c1 in string with c2
  59. ****************************************************************/
  60. replace_char (string, c1, c2)
  61. char *string, c1, c2;
  62. {
  63. int i;
  64.  
  65. for (i=0; string[i] != '\0'; i++)
  66.     if (string[i] == c1)    string[i] = c2;
  67.  
  68. return;
  69. }
  70.  
  71.  
  72. /********************************************************************
  73.         produces n empty lines
  74. *********************************************************************/
  75. skip (n)
  76. int n;
  77. {
  78. int i;
  79.  
  80. if DEBUG fprintf(STDERR,"\n    SKIP %d line(s)", n);
  81.  
  82. for ( i=0; i<n; i++)
  83.       { if DEBUG putchar ('!');
  84.     putchar (NEWLINE);
  85.       }
  86.  
  87. return;
  88. }
  89.  
  90.  
  91. /******************************************************************
  92.     indents the proper number of spaces
  93. *******************************************************************/
  94. indent(val)
  95. int val;
  96. {
  97. int i;
  98.  
  99. if DEBUG fprintf(STDERR,"\n    INDENT %d spaces(s)",val);
  100.  
  101.     for ( i=0; i<val; i++ )        putchar( BLANK );
  102.  
  103.  
  104. }
  105.  
  106.  
  107.  
  108. /*******************************************************************
  109.         puts out page header
  110. *******************************************************************/
  111. phead()
  112. {
  113.  
  114.  
  115. CURPAG = NEWPAG;
  116. NEWPAG++;
  117.  
  118. if ( M1VAL > 0 )
  119.       { skip ( M1VAL - 1 );
  120.     puttl ( HEADER, CURPAG );
  121.       }
  122.  
  123. skip ( M2VAL );
  124.  
  125. LINENO = M1VAL + M2VAL + 1;
  126. if DEBUG fprintf(STDERR,"\nLINENO=%d", LINENO);
  127. return;
  128. }
  129.  
  130.  
  131. /*********************************************************************
  132.         puts out page footer
  133. *********************************************************************/
  134. pfoot()
  135. {
  136.  
  137.  
  138. skip ( M3VAL );
  139. if ( M4VAL > 0 )
  140.       { puttl ( FOOTER, CURPAG );
  141.     skip ( M4VAL - 1 );
  142.       }
  143. return;
  144.  
  145. }
  146.  
  147.  
  148. /*******************************************************************
  149.     put out title line with optional page no.
  150. *******************************************************************/
  151. puttl ( title_str, pageno )
  152. char *title_str;
  153. int pageno;
  154. {
  155. int i;
  156.  
  157.  
  158. for ( i=0; title_st[i] != '\0'; i++ )
  159.     if ( title_str[i] == NUMSIGN )
  160.         putdec ( pageno, 1 );    /* print pageno, width >= 1 */
  161.     else
  162.         putchar( title_str[i]);
  163.  
  164. return;
  165. }
  166.  
  167. /*******************************************************************
  168.     put out num in string of width >= w
  169. ******************************************************************/
  170. putdec ( num, w )
  171. int num;
  172. int w;
  173. {
  174. int i, nd;
  175. char chars[10];
  176.  
  177.  
  178. nd = itoc ( num, chars, 10 );
  179. for ( i=nd + 1; i<=w; i++ )
  180.     putchar (BLANK);
  181. for ( i=0;i<=nd; i++)
  182.     putchar ( chars[i] );
  183.  
  184. return;
  185. }
  186.  
  187. /*******************************************************************
  188.     convert int num to char string in numstr
  189. *********************************************************************/
  190. itoc ( num, numstr, size )
  191. int num;
  192. char *numstr;
  193. int size;    /* largest size of numstr */
  194. {
  195. int absnum, i, j, k, d;
  196.  
  197.  
  198. absnum = abs (num);
  199. numstr[0] = '\0';
  200. i = 0;
  201.  
  202. do
  203.       { i++;
  204.     d = absnum % 10;
  205.     numstr[i] = d + '0';
  206.     absnum = absnum/10;
  207.       } while ( absnum != 0 && i<size );
  208.  
  209. if ( num < 0 && i<size )
  210.       { i++;
  211.     numstr[i] = '-';
  212.       }
  213.  
  214. for ( j=0; j<i; j++ )
  215.       { k = numstr[i];
  216.     numstr[i] = numstr[j];
  217.     numstr[j] = k;
  218.     i--;
  219.       }
  220.  
  221. return ( strlen(numstr) );
  222. }
  223.  
  224. /********************************************************************
  225.     copy title from com_line to ttl
  226. **********************************************************************/
  227. gettl ( com_line, ttl )
  228. char *com_line, *ttl;
  229. {
  230. int i;
  231. char local[ MAXLINE ];
  232.  
  233.  
  234. if DEBUG fprintf(STDERR,"\n\nGETTL command line= <%s>", com_line);
  235.  
  236. i=0;
  237. while ( com_line[i]!= ' ' && com_line[i]!='\t' && com_line[i]!='\n')
  238.     i++;
  239.  
  240. strcpy ( local, com_line );
  241. skip_blanks (&local[i]);
  242.  
  243.     /* strip quote if found */
  244. if ( local[i]==SQUOTE || local[i]==DQUOTE)    i++;
  245.  
  246. strcpy ( ttl, &local[i] );
  247. if DEBUG fprintf(STDERR,"\ntitle = <%s>", ttl);
  248.  
  249. return;
  250. }
  251.  
  252.  
  253. /******************************************************************
  254.     space n lines or to bottom of the page
  255. *******************************************************************/
  256. space (n)
  257. int n;
  258. {
  259.  
  260. if DEBUG fprintf(STDERR,"\nSPACE %d line(s), LINENO= %d", n, LINENO);
  261.  
  262. brk();    /* flush out last unfilled line */
  263. if (LINENO > BOTTOM)    return;    /* end of page */
  264.  
  265. if ( LINENO == 0 )    /* top of page */
  266.     phead();
  267.  
  268. skip( min( n, BOTTOM+1-LINENO ));    /* can't skip past bottom  */
  269. LINENO = LINENO + n;    /* obvious */
  270.  
  271. if DEBUG fprintf(STDERR,"\n    LINENO = %d", LINENO);
  272. if (LINENO > BOTTOM)
  273.       {
  274.     pfoot();    /* print footer if bottom */
  275.       }
  276.  
  277. return;
  278. }
  279.  
  280.  
  281. /*******************************************************
  282.     yet ANOTHER version of text !!! no. 59,999,999
  283. get it right this time Kath dear
  284. *******************************************************/
  285. text (line)
  286. char *line;
  287. {
  288. char wrdbuf [MAXLINE];
  289. int i, j, k;
  290. char *p1, *p2;
  291.  
  292. if DEBUG fprintf(STDERR,"\n\nTEXT:<%s>", line);
  293. if (line[0] == BLANK || line[0] == NEWLINE || line[0] == TAB)
  294.     leadbl (line);
  295. if (ULVAL > 0)    /* set high bits of all non-white space chars */
  296.       {
  297.     ULVAL--;
  298.     p1 = p2 = line;
  299.     while (*p2)
  300.           { if (*p2 == TAB || *p2 == BLANK || *p2 == NEWLINE)
  301.             *p1++ = *p2++;
  302.         else 
  303.             *p1++ = *p2++ | 0x80;
  304.           }
  305.       }
  306. if (CEVAL > 0)
  307.       { center (line);
  308.     put (line);
  309.     CEVAL--;
  310.       }
  311. else if ( line[0] == NEWLINE || FILL == NO )
  312.     put (line);
  313. else while (WE_HAVE_A_WORD == getwrd (line, wrdbuf))
  314.     putwrd (wrdbuf);
  315. return;
  316. }
  317.  
  318. /***********************************************************
  319.     put out a line of text with correct indentation
  320.     underlining if specified
  321. ************************************************************/
  322. put (line)
  323. char *line;
  324. {
  325. int i, j, k;
  326.  
  327. if DEBUG fprintf(STDERR,"\n\nPUT<%s>",line);
  328. if (LINENO == 0 || LINENO > BOTTOM )    phead();
  329. indent (TIVAL);
  330. putline (line);
  331. TIVAL = INVAL;
  332. skip (min (LSVAL-1, BOTTOM-LINENO));
  333. LINENO = LINENO + LSVAL;
  334. if DEBUG fprintf(STDERR,"\nLINENO=%d,  LSVAL=%d",LINENO, LSVAL);
  335. if (LINENO > BOTTOM)    pfoot();
  336. return;
  337. }
  338. /***********************************************************
  339. concatenates the word onto the end of OUTBUF for filled text
  340. ************************************************************/
  341. putwrd (wrdbuf)
  342. char *wrdbuf;
  343. {
  344. int i, j, k;
  345. char s[MAXLINE], ch;
  346. int line_len, outw, new_out_width, wid;
  347. int nextra;
  348.  
  349. if DEBUG fprintf(STDERR,"\nwrdbuf = <%s>",wrdbuf);
  350. skip_blanks (wrdbuf); trunc_bl (wrdbuf);
  351. wid = strlen (wrdbuf);
  352. if DEBUG fprintf(STDERR,"\nwid = %d",wid);
  353.  
  354. line_len = RMVAL - TIVAL;
  355. outw = strlen (OUTBUF);
  356. new_out_width = wid + outw + 1;    /* one for blank */
  357. if DEBUG fprintf(STDERR,"\nnew_out_width=%d, outw=%d, wid=%d, line_len=%d",
  358.                    new_out_width,    outw,    wid,    line_len   );
  359. if (new_out_width > min (line_len, MAXLINE-1))
  360.       { nextra = min (line_len, MAXLINE-1) -outw + 1;
  361.     spread (OUTBUF, nextra, OUTWRDS);
  362.     brk();
  363.       }
  364.  
  365. strcat (OUTBUF, wrdbuf);
  366. strcat (OUTBUF, " ");
  367. if DEBUG fprintf(STDERR,"\nPUTWRD:OUTBUF=<%s>",OUTBUF);
  368. OUTWRDS++;
  369. return;
  370. }
  371.  
  372. /**************************************************************
  373.     returns no. of ch in string
  374. ***************************************************************/
  375. int count_char (string, ch)
  376. char *string, ch;
  377. {
  378. int n;
  379. char *p;
  380.  
  381. p = string;
  382. n=0;
  383. while (*p)    if (*p++ == ch)    n++;
  384. return (n);
  385. }
  386.  
  387. /***********************************************************
  388.     remove all occurrences of ch; first, last, and no.
  389.     of occurrences returned in parameters
  390. ************************************************************/
  391. remove_char (string, ch, first, last, n_ch)
  392. char *string, ch;
  393. int *first, *last, *n_ch;
  394. {
  395. char *p1, *p2;
  396. find_char (string, ch, first, last);
  397. *n_ch = count_char (string, ch);
  398. p1=p2=string;
  399. while (*p1 = *p2++)    if (*p1 != ch)    p1++;
  400. return;
  401. }
  402.  
  403. /**************************************************************
  404.     a new putline routine; sends line to current output
  405. ***************************************************************/
  406. putline (line)
  407. char *line;
  408. {
  409. int i;
  410.  
  411. if (ULVAL >=0)
  412.       { replace_char (line, NEWLINE, CR);
  413.     puts (line);
  414.     indent (TIVAL);
  415.     for (i=0; line[i] != '\0'; i++)
  416.         if (line[i] & 0x80)    putchar (UNDERLINE);
  417.         else putchar (line[i]);
  418.     putchar (CR);
  419.     putchar (NEWLINE);
  420.     if (ULVAL==0)    ULVAL--;
  421.       }
  422. else puts (line);
  423. return;
  424. }
  425. else putchar (line[i]);
  426.     putchar (CR);
  427.     putchar (NEWLINE);
  428.     if (ULVAL==0)    ULVAL--;
  429.       }